package future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @projectName: JUC
 * @package: future
 * @className: futureDemo
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/4 1:26
 * @version: 1.0
 */
public class futureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ////没有返回值的异步回调， runAsync
        //CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //    System.out.println(Thread.currentThread().getName() + "runAsync=> Void");
        //});
        //System.out.println("1111");
        ////获取执行结果
        //completableFuture.get();

        //    有返回值的
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "runAsync=>integer");
            int i = 10 / 0;
            return 1024;
        });
        completableFuture.whenComplete((t, u) -> {
            //t是正常的返回结果
            //u是返回报错信息
            System.out.println("t=>" + t);
            System.out.println("u=>" + u);
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 233;
        }).get();
    }
}
